package com.yj.springbootmybatis.service;

import cn.hutool.core.util.DesensitizedUtil;
import com.yj.springbootmybatis.enums.CommentType;
import com.yj.springbootmybatis.mapper.*;
import com.yj.springbootmybatis.pojo.*;
import com.yj.springbootmybatis.pojo.vo.CommentLevelCountsVO;
import com.yj.springbootmybatis.pojo.vo.ItemCommentsVO;
import com.yj.springbootmybatis.pojo.vo.ItemSearchVO;
import com.yj.springbootmybatis.pojo.vo.ShopcartVO;
import com.yj.springbootmybatis.util.PageUtils;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ItemServiceImpl implements ItemService {


    @Autowired
    private ItemsMapper itemsMapper;

    @Autowired
    private ItemsImgMapper itemsImgMapper;

    @Autowired
    private ItemsParamMapper itemsParamMapper;

    @Autowired
    private ItemsSpecMapper itemsSpecMapper;

    @Autowired
    private ItemsCommentsMapper itemsCommentsMapper;

    @Autowired
    private ItemsCommentsCustomMapper itemsCommentsCustomMapper;

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public Items queryItemByItemId(Integer itemId) {
        Example example = new Example(Items.class);
        example.createCriteria().andEqualTo("id",itemId);
        Items items = itemsMapper.selectOneByExample(example);
        return items;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<ItemsImg> queryItemImagByItemId(Integer itemId) {

        Example example = new Example(ItemsImg.class);
        example.createCriteria().andEqualTo("itemId",itemId);
        List<ItemsImg> list = itemsImgMapper.selectByExample(example);
        return list;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<ItemsSpec> queryItemSpecByItemId(Integer itemId) {
        Example example = new Example(ItemsSpec.class);
        example.createCriteria().andEqualTo("itemId",itemId);
        List<ItemsSpec> list = itemsSpecMapper.selectByExample(example);
        return list;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public ItemsParam queryItemParamByItemId(Integer itemId) {
        Example example = new Example(ItemsParam.class);
        example.createCriteria().andEqualTo("itemId",itemId);
        ItemsParam itemsParam = itemsParamMapper.selectOneByExample(example);
        return itemsParam;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public CommentLevelCountsVO queryCommentCounts(Integer itemId) {

        //1、获取好评记录数
        //2、获取中评记录数
        //3、获取差评记录数
        CommentLevelCountsVO commentLevelCountsVO = new CommentLevelCountsVO();
        Integer goodCommentCounts = getCommentCounts(itemId, CommentType.GOOD.type);
        Integer normalCommentCounts = getCommentCounts(itemId, CommentType.NORMAL.type);
        Integer badCommentCounts = getCommentCounts(itemId, CommentType.BAD.type);
        Integer totals = goodCommentCounts + normalCommentCounts +badCommentCounts;
        commentLevelCountsVO.setGoodTotals(goodCommentCounts);
        commentLevelCountsVO.setNormalTotals(normalCommentCounts);
        commentLevelCountsVO.setBadTotals(badCommentCounts);
        commentLevelCountsVO.setTotals(totals);
        return commentLevelCountsVO;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public PageInfo queryCommentsByItemIdAndLevel(Integer itemId, Integer level, Integer pageNum, Integer pageSize) {

        Map<String,Object> map = new HashMap<>();
        map.put("commentLevel",level);
        map.put("itemId",itemId);

        List<ItemCommentsVO> list = itemsCommentsCustomMapper.queryItemComments(map);

        for (ItemCommentsVO itemCommentsVO : list) {
            itemCommentsVO.setNickname(DesensitizedUtil.chineseName(itemCommentsVO.getNickname()));
        }

        PageInfo pageList = PageUtils.getPageList(pageNum, pageSize, list);

        return pageList;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public PageInfo queryItemByLikeItemName(Integer pageNum,Integer pageSize,String itemName, String sort) {
        Map<String,Object> map = new HashMap<>();
        map.put("itemName",itemName);
        map.put("sort",sort);
        List<ItemSearchVO> list = itemsCommentsCustomMapper.queryItemByLikeItemName(map);
        PageInfo pageList = PageUtils.getPageList(pageNum, pageSize, list);

        return pageList;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public PageInfo queryItemByCatId(Integer pageNum,Integer pageSize,Integer catId, String sort) {
        Map<String,Object> map = new HashMap<>();
        map.put("catId",catId);
        map.put("sort",sort);
        List<ItemSearchVO> list = itemsCommentsCustomMapper.queryItemByCatId(map);
        PageInfo pageList = PageUtils.getPageList(pageNum, pageSize, list);

        return pageList;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<ShopcartVO> queryShopCartBySpecIds(List<String> specIds) {
        return itemsCommentsCustomMapper.queryShopCartBySpecIds(specIds);
    }


    @Transactional(propagation = Propagation.SUPPORTS)
    Integer getCommentCounts(Integer itemId,Integer level){
        ItemsComments itemsComments = new ItemsComments();
        itemsComments.setItemId(itemId);
        if (level!=null){
            itemsComments.setCommentLevel(level);
        }
        int results = itemsCommentsMapper.selectCount(itemsComments);
        return results;
    }

}
